/*****************************************************************************
 * input.c: JNI native input functions for VLC Java Bindings
 *****************************************************************************
 * Copyright (C) 1998-2006 The VideoLAN team
 * Copyright (C) 2007 The LegendTV Project
 *
 * Authors: Filippo Carone <filippo@carone.org>
 * 			Guy Paddock <gap7472@rit.edu>
 *
 * $Id $
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

#include <jni.h>
#include <vlc/libvlc.h>

#include "../faults/jFaults.h"	// JNI fault tolerance

#include "generated/Input.h"	// JVLC internal imports, generated by gcjh
#include "exceptions.h"

libvlc_input_t *JVLC_getInputThread(JNIEnv *env, libvlc_instance_t* instance, libvlc_exception_t* exception)
{
	libvlc_input_t*	retVal;
		
	retVal	= libvlc_playlist_get_input(instance, exception);
	
	CHECK_EXCEPTION;
	
	return retVal;
}

JNIEXPORT jlong JNICALL Java_com_googlecode_legendtv_core_Input_getLength(JNIEnv *env, jobject _this) 
{
	jlong	retVal	= 0;
	
	JFAULT_TOLERANT_SCOPE_WITH_RETURN(env,
	{
		libvlc_exception_t*	exception	= JVLC_initException();
		libvlc_instance_t*	instance	= JVLC_getIntfInstance(env, _this);
		libvlc_input_t*		input		= JVLC_getInputThread(env, instance, exception);
		
	    retVal	= libvlc_input_get_length(input, exception);
	    
	    CHECK_EXCEPTION_FREE;
	}, retVal);
	
	return retVal;
}

JNIEXPORT jlong JNICALL Java_com_googlecode_legendtv_core_Input_getTime(JNIEnv *env, jobject _this) 
{
	jlong	retVal	= 0;
	
	JFAULT_TOLERANT_SCOPE_WITH_RETURN(env,
	{
	    libvlc_exception_t*	exception	= JVLC_initException();
		libvlc_instance_t*	instance	= JVLC_getIntfInstance(env, _this);
		libvlc_input_t*		input		= JVLC_getInputThread(env, instance, exception);
		
		retVal	= libvlc_input_get_time(input, exception);
	    
	    CHECK_EXCEPTION_FREE;
	}, retVal);
	
	return retVal;
}

JNIEXPORT void JNICALL Java_com_googlecode_legendtv_core_Input_setTime(JNIEnv *env, jobject _this, jlong time) 
{
	JFAULT_TOLERANT_SCOPE(env,
	{
	    libvlc_exception_t*	exception	= JVLC_initException();
		libvlc_instance_t*	instance	= JVLC_getIntfInstance(env, _this);
		libvlc_input_t*		input		= JVLC_getInputThread(env, instance, exception);
	
	    libvlc_input_set_time(input, time, exception);
	    
	    CHECK_EXCEPTION_FREE;
	});
}

JNIEXPORT jfloat JNICALL Java_com_googlecode_legendtv_core_Input_getPosition(JNIEnv *env, jobject _this) 
{	
	jfloat	retVal	= 0;
	
	JFAULT_TOLERANT_SCOPE_WITH_RETURN(env,
	{
		libvlc_exception_t*	exception	= JVLC_initException();
		libvlc_instance_t*	instance	= JVLC_getIntfInstance(env, _this);
		libvlc_input_t*		input		= JVLC_getInputThread(env, instance, exception);
		
		retVal	= libvlc_input_get_position(input, exception);
		
		CHECK_EXCEPTION_FREE;
	}, retVal);
	
	return retVal;
}

JNIEXPORT void JNICALL Java_com_googlecode_legendtv_core_Input_setPosition(JNIEnv *env, jobject _this, jfloat position )
{
	JFAULT_TOLERANT_SCOPE(env,
	{
	    libvlc_exception_t*	exception	= JVLC_initException();
		libvlc_instance_t*	instance	= JVLC_getIntfInstance(env, _this);
		libvlc_input_t*		input		= JVLC_getInputThread(env, instance, exception);
	    
	    libvlc_input_set_position(input, position, exception);
	    
	    CHECK_EXCEPTION_FREE;
	});
}

JNIEXPORT jboolean JNICALL Java_com_googlecode_legendtv_core_Input_isPlaying(JNIEnv *env, jobject _this) 
{
	jboolean	retVal	= 0;
	
	JFAULT_TOLERANT_SCOPE_WITH_RETURN(env,
	{
		libvlc_exception_t*	exception	= JVLC_initException();
		libvlc_instance_t*	instance	= JVLC_getIntfInstance(env, _this);
		libvlc_input_t*		input		= JVLC_getInputThread(env, instance, exception);
		
		retVal		= libvlc_input_will_play(input, exception);
		
		CHECK_EXCEPTION_FREE;
	}, retVal);
	
	return retVal;
}

JNIEXPORT jfloat JNICALL Java_com_googlecode_legendtv_core_Input_getFPS(JNIEnv *env, jobject _this) 
{
	jfloat	retVal	= 0;
		
	JFAULT_TOLERANT_SCOPE_WITH_RETURN(env,
	{	
		libvlc_exception_t*	exception	= JVLC_initException();
		libvlc_instance_t*	instance	= JVLC_getIntfInstance(env, _this);
		libvlc_input_t*		input		= JVLC_getInputThread(env, instance, exception);
		
		retVal		= libvlc_input_get_fps(input, exception);
		
		CHECK_EXCEPTION_FREE;
	}, retVal);

	return retVal;
}

JNIEXPORT jboolean JNICALL Java_com_googlecode_legendtv_core_Input_hasVout(JNIEnv *env, jobject _this) 
{
	jboolean	retVal = 0;
	
	JFAULT_TOLERANT_SCOPE_WITH_RETURN(env,
	{
		libvlc_exception_t*	exception	= JVLC_initException();
		libvlc_instance_t*	instance	= JVLC_getIntfInstance(env, _this);
		libvlc_input_t*		input		= JVLC_getInputThread(env, instance, exception);
		
		retVal		= libvlc_input_has_vout(input, exception);
		
		CHECK_EXCEPTION_FREE;
	}, retVal);
	
	return retVal;	
}
